#include "Common.hpp"


int main()
{
    // 创建一个命名管道
    Fifo fifo(Path);
    
    int rfd = open(Path, O_RDONLY);
    if(rfd < 0)
    {
        cerr << "when read, open failed, errno: " << errno << ", errstring: " << strerror(errno) << endl;
        return 1;
    }

    // 如果写端没被打开，读端打开时，open会被阻塞，直到写端被打开
    cout << "read open success" << endl;

    char buffer[1024];
    while(true)
    {
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            // 将读取到的内容打印出来
            cout << "client say: " << buffer << endl;
        }
        else if(n == 0)
        {
            // 写端被关闭，没有读取到内容，就退出
            cout << "The client has been shut dowm, and the server will be shut down" << endl;
            break;
        }
        else 
        {
            cerr << "read error, errno: " << errno << ", errstring: " << strerror(errno) << endl;
            break;
        }
    }
    close(rfd);

    return 0;
}
